﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Services.Localization;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Xml;

    public class CategoryArrayList
    {
        protected ArrayList CategoryArray;

        public CategoryArrayList(int PortalID)
        {
            IEnumerator enumerator;
            CategoryController controller = new CategoryController();
            ArrayList categories = new ArrayList();
            LocaleCollection validLocales = SharedFunctions.GetValidLocales();
            this.CategoryArray = new ArrayList();
            try
            {
                enumerator = validLocales.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    IEnumerator enumerator2;
                    string lang = Conversions.ToString(enumerator.Current);
                    categories = controller.GetCategories(PortalID, lang);
                    try
                    {
                        enumerator2 = categories.GetEnumerator();
                        while (enumerator2.MoveNext())
                        {
                            NB_Store_CategoriesInfo current = (NB_Store_CategoriesInfo) enumerator2.Current;
                            current.CategoryName = current.CategoryName;
                            this.CategoryArray.Add(current);
                        }
                    }
                    finally
                    {
                        if (enumerator2 is IDisposable)
                        {
                            (enumerator2 as IDisposable).Dispose();
                        }
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
        }

        public CategoryArrayList(XmlDataDocument xmlDoc)
        {
            IEnumerator enumerator;
            ArrayList list = new ArrayList();
            NB_Store_CategoriesInfo info = new NB_Store_CategoriesInfo();
            LocaleCollection validLocales = SharedFunctions.GetValidLocales();
            try
            {
                enumerator = validLocales.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    IEnumerator enumerator2;
                    XmlNodeList list2 = xmlDoc.SelectNodes("root/categories/" + Conversions.ToString(enumerator.Current) + "/NB_Store_CategoriesInfo");
                    try
                    {
                        enumerator2 = list2.GetEnumerator();
                        while (enumerator2.MoveNext())
                        {
                            XmlNode current = (XmlNode) enumerator2.Current;
                            info = (NB_Store_CategoriesInfo) XmlUtils.Deserialize(current.OuterXml, info.GetType());
                            if (info != null)
                            {
                                info.CategoryName = info.CategoryName;
                                list.Add(info);
                            }
                        }
                    }
                    finally
                    {
                        if (enumerator2 is IDisposable)
                        {
                            (enumerator2 as IDisposable).Dispose();
                        }
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            this.CategoryArray = list;
        }

        public NB_Store_CategoriesInfo GetCat(int CatID, string Lang)
        {
            IEnumerator enumerator;
            try
            {
                enumerator = this.CategoryArray.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    NB_Store_CategoriesInfo current = (NB_Store_CategoriesInfo) enumerator.Current;
                    if ((current.CategoryID == CatID) & (current.Lang == Lang))
                    {
                        return current;
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            return null;
        }

        public NB_Store_CategoriesInfo GetCatByName(string CatQualifiedName, string Lang)
        {
            IEnumerator enumerator;
            try
            {
                enumerator = this.CategoryArray.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    NB_Store_CategoriesInfo current = (NB_Store_CategoriesInfo) enumerator.Current;
                    if (this.GetCatQualifiedName(current.CategoryID, Lang) == CatQualifiedName)
                    {
                        return current;
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            return null;
        }

        public string GetCatQualifiedName(int CategoryID, string Lang)
        {
            IEnumerator enumerator;
            try
            {
                enumerator = this.CategoryArray.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    NB_Store_CategoriesInfo current = (NB_Store_CategoriesInfo) enumerator.Current;
                    if ((current.Lang == Lang) & (current.CategoryID == CategoryID))
                    {
                        if ((current.ParentCategoryID > 0) & (current.ParentCategoryID != CategoryID))
                        {
                            return (this.GetCatQualifiedName(current.ParentCategoryID, Lang) + "/" + Strings.Left(current.CategoryName, 50));
                        }
                        return Strings.Left(current.CategoryName, 50);
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            return "";
        }
    }
}

